package com.dao;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;


public abstract class JdbcDAO extends IJdbcDAO {
	@Resource(name="jdbcTemplate")
	protected JdbcTemplate  jdbc;

	@Override
	public void delete(String sql) {
		// TODO Auto-generated method stub
		jdbc.update(sql);
	}
	
	@Override
	public void delete(String sql, Object[] args) {
		// TODO Auto-generated method stub
		jdbc.update(sql, args);
	}

	@Override
	public void save(String sql, Object[] args) {
		// TODO Auto-generated method stub
		jdbc.update(sql, args);
	}
	@Override
	public KeyHolder saveandgetkey(PreparedStatementCreator psc){
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbc.update(psc, keyHolder); 
	    return keyHolder;
	}

	public void update(String sql) {
		// TODO Auto-generated method stub
		jdbc.update(sql);
	}
	@Override
	public void update(String sql, Object[] args) {
		// TODO Auto-generated method stub
		jdbc.update(sql, args);
	}
	@Override
	public void update(String sql, PreparedStatementSetter pss) {
		// TODO Auto-generated method stub
		jdbc.update(sql, pss);
	}

	@Override
	public int getCount(String sql, Object[] args) {
		return jdbc.queryForInt(sql,args);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List getList(String sql, Object[] args,RowMapper rowMapper)
	{
		return jdbc.query(sql, args,rowMapper);
	}
	

	@Override
	@SuppressWarnings("unchecked")
	public Object getObject(String sql, Object[] args,Class requiredType)
	{
		try{
			return jdbc.queryForObject(sql,args, requiredType);
		}catch (org.springframework.dao.EmptyResultDataAccessException e) {
			// TODO: handle exception
			return null;
		}
	}
	
	@Override
	public Object getObject(String sql, Object[] args,RowMapper rowMapper)
	{
		try{
			return jdbc.queryForObject(sql, args, rowMapper);
		}catch (org.springframework.dao.EmptyResultDataAccessException e) {
			// TODO: handle exception
			return null;
		}
	}
	
}
